package edu.bu.cs683.traveldroid;

import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;

import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.Projection;

/**
 * This class implements an Overlay object that displays a Route.  A route is
 * drawn as a path made up of all the points in the Route.
 */
public class RouteOverlay extends Overlay {

	private List<GeoPoint> routePoints;
	
	/**
	 * Constructor.
	 * 
	 * @param theRoute
	 */
	public RouteOverlay( List<GeoPoint> theRoute ){
		routePoints = theRoute;
	}
	
	/**
	 * Draws the Route
	 */
	@Override
	public boolean draw (Canvas canvas, MapView mapView, boolean shadow, long when){
		Projection projection = mapView.getProjection();
		
		if( shadow || (routePoints.size()<2) ){
			return super.draw(canvas, mapView, shadow, when);
		}
		
		// Set paint properties
		Paint paint = new Paint();
		paint.setColor(Color.MAGENTA);
		paint.setStrokeWidth(5);
		paint.setAlpha(120);
		paint.setStyle(Paint.Style.STROKE);
		paint.setAntiAlias(true);
		
		// Create path and set the start location
		Path path = new Path();
		Point startPoint = new Point();
		projection.toPixels(routePoints.get(0), startPoint);
		path.moveTo(startPoint.x, startPoint.y);
		
		// Iterate through each route point
		for( int i=1; i< routePoints.size(); i++){
			// Add line from last point to this point
			Point point = new Point();
			projection.toPixels(routePoints.get(i), point);
			path.lineTo(point.x, point.y);
		}
		
		// Draw the route
		canvas.drawPath(path, paint);
		
		return super.draw(canvas, mapView, shadow, when);
	}
}
